//
//  HJLoginController.m
//  KaiChat
//
//  Created by lx on 2023/10/16.
//

#import "HJLoginController.h"
#import "HJTitleTextField.h"
#import "QMUIButton.h"
#import "HJMinePresent.h"
#import "HJVersionUpdate.h"
#import "HJWebSocketManager.h"

@interface HJLoginController ()

@property (nonatomic, weak) HJTitleTextField *passwordTextField;
@property (nonatomic, weak) HJTitleTextField *emailTextField;
@property (nonatomic, weak) HJStyle1Button *emailLoginButton;

@end



@implementation HJLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
 

    
    UIImageView *topBackgroundImageView = [UIImageView addToView:self.view];
    topBackgroundImageView.image = [UIImage imageNamed:@"logo1"];
    [topBackgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.offset(KNavBarHight + 8);
    }];
    
    UILabel *titleLabel = [UILabel lableWithSuperView:self.view font:UIFontMediumMake(16) color:UIColor.tj_333333Color title:@"Keep Accelerating IM" textAlignment:NSTextAlignmentLeft];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topBackgroundImageView.mas_bottom).offset(2);
        make.centerX.offset(0);
    }];
    
    HJTitleTextField *emailTextField = [HJTitleTextField addToView:self.view];
    emailTextField.titleLabel.text = @"Email address";
    emailTextField.textField.placeholder = @"Enter your email address";
    emailTextField.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    emailTextField.textField.text = HJUserInfo.email;
    emailTextField.textField.keyboardType = UIKeyboardTypeASCIICapable;
    [emailTextField.textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [emailTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(leftRightMargin);
        make.top.equalTo(titleLabel.mas_bottom).offset(40);
        make.height.offset(82);
    }];
    
    HJTitleTextField *passwordTextField = [HJTitleTextField addToView:self.view];
    passwordTextField.titleLabel.text = @"Password";
    passwordTextField.textField.placeholder = @"Enter Password";
    passwordTextField.secureTextEntry = YES;
    passwordTextField.textField.keyboardType = UIKeyboardTypeASCIICapable;
    passwordTextField.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [passwordTextField.textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(leftRightMargin);
        make.top.equalTo(emailTextField.mas_bottom).offset(20);
        make.height.offset(82);
    }];
 
    QMUIButton *forgotButton = [QMUIButton buttonWithSuperView:self.view font:UIFontMediumMake(12) color:UIColorMake(0x8667FE) title:@"Forgot Password?"];
   [forgotButton sizeToFit];
    CGSize buttonSize = forgotButton.tj_size;
    [forgotButton addTarget:self action:@selector(didClickForgotButton) forControlEvents:UIControlEventTouchUpInside];
    [forgotButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(passwordTextField);
        make.width.offset(buttonSize.width);
        make.height.offset(buttonSize.height);
        make.top.equalTo(passwordTextField.mas_bottom).offset(20);
    }];
    
    HJStyle1Button *emailLoginButton = [HJStyle1Button buttonWithSuperView:self.view font:UIFontMediumMake(14) color:nil title:@"Log in"];
    emailLoginButton.enabled = NO;
    [emailLoginButton addTarget:self action:@selector(didClickLoginButton) forControlEvents:UIControlEventTouchUpInside];
    [emailLoginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(passwordTextField);
        make.height.offset(buttonHeight);
        make.bottom.inset(10 + TabbarSafeBottomMargin);
    }];

//    emailTextField.textField.text = @"test1600@gmail.com";
//    passwordTextField.textField.text = @"a12345678!";
//
    emailTextField.textField.text = @"tj23157@gmail.com";
    passwordTextField.textField.text = @"Xtj23157@";
    
    _emailTextField = emailTextField;
    _passwordTextField = passwordTextField;
    _emailLoginButton = emailLoginButton;
    
    [self textFieldDidChange:_emailTextField.textField];
    
    [HJVersionUpdate checkVersionUpdateInController:self];

}

- (void)textFieldDidChange:(UITextField *)textField {
    _emailLoginButton.enabled = !(isEmptyString(_emailTextField.textField.text) || isEmptyString(_passwordTextField.textField.text));
}


- (void)didClickForgotButton {
    [self.navigationController pushViewControllerWithName:@"HJForgotPasswordController"];
}
 
- (void)didClickLoginButton {
    
    NSString *account = _emailTextField.textField.text;
    NSString *password = _passwordTextField.textField.text;
    
    if (isEmptyString(account)) {
        [HJHUD showErrorMessage:_emailTextField.textField.placeholder];
        return;
    }
    
    if (isEmptyString(password)) {
        [HJHUD showErrorMessage:_passwordTextField.textField.placeholder];
        return;
    }
    
    [HJHUD showLoadingInView:self.view];
    [HJMinePresent loginWithAccount:account password:password complete:^(NSError * _Nullable error) {
        if (!error) {
            [HJWebSocketManager.shareManager connect];
        }
    }];
}

- (BOOL)hiddenBackgroundView {
    return NO;
}

@end
